package login;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class delete {
    public delete() {
        JButton c1, c2;
        JTextField b1;
        JPanel nn;
        JPasswordField b2;
        JFrame nw = new JFrame("注销");
        nn = new JPanel();
        //确定
        c1 = new JButton("确定");
        c1.setFont(new Font("宋体", Font.PLAIN, 26));
        c1.setBounds(120, 240, 100, 59);
        //取消
        c2 = new JButton("取消");
        c2.setFont(new Font("宋体", Font.PLAIN, 26));
        c2.setBounds(270, 240, 100, 59);
        nn.setLayout(null);
        //用户名
        JLabel a1 = new JLabel("用户名");
        a1.setFont(new Font("宋体", Font.PLAIN, 30));
        a1.setBounds(54, 15, 137, 45);
        nn.add(a1);
        //用户名编辑框
        b1 = new JTextField(20);
        b1.setBounds(175, 21, 223, 29);
        nn.add(b1);
        //密码
        JLabel a2 = new JLabel("密  码");
        a2.setFont(new Font("宋体", Font.PLAIN, 30));
        a2.setBounds(54, 65, 137, 45);
        nn.add(a2);
        //密码输入框
        b2 = new JPasswordField(20);
        b2.setBounds(175, 73, 223, 29);
        nn.add(b2);

        nn.add(c1);
        nn.add(c2);
        nw.getContentPane().add(nn);
        nw.setVisible(true);
        //设置窗口的大小和位置
        nw.setSize(500, 500);
        nw.setLocation(350, 150);
        c1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String name = (b1.getText());
                String passwords = (b2.getText());
                dbhelper s1 = new dbhelper();
                String sql = "SELECT * FROM user1 WHERE name ='" + name + "'and passwords='" + passwords + "'";
                String sql1 = "delete from user1 where name=?";
                try {
                    Statement stmt = (Statement) s1.connection.createStatement();
                    ResultSet rs = (ResultSet) stmt.executeQuery(sql);
                    PreparedStatement prestmt = (PreparedStatement) s1.connection.prepareStatement(sql1);
                    if (rs.next() == true) {
                        prestmt.setString(1, name);
                        prestmt.executeUpdate();
                        System.out.println("删除数据成功！");
                        prestmt.close();
                        JOptionPane.showMessageDialog(null, "注销成功！", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
                        nw.setVisible(false);
                    } else {
                        JOptionPane.showMessageDialog(null, "注销失败：请检查用户和密码是否正确！", "提示窗口", JOptionPane.WARNING_MESSAGE);
                    }
                    stmt.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });
        c2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                b1.setText("");
                b2.setText("");
            }
        });

    }

    public static void main(String[] args) {
        new delete();
    }
}
